package com.atguli.gulimall.product.service.impl;

import com.atguli.common.entity.QueryWrapperVO;
import com.atguli.gulimall.product.dao.BrandDao;
import com.atguli.gulimall.product.dao.CategoryDao;
import com.atguli.gulimall.product.entity.BrandEntity;
import com.atguli.gulimall.product.entity.CategoryEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguli.common.utils.PageUtils;
import com.atguli.common.utils.Query;

import com.atguli.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguli.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguli.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
@Slf4j
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Resource
    BrandDao brandDao;
    @Resource
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(QueryWrapperVO params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存所有字段
     *
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        final Long brandId = categoryBrandRelation.getBrandId();
        final Long catelogId = categoryBrandRelation.getCatelogId();
        log.info("通过品牌ID和分类ID查询");
        final BrandEntity brandEntity = brandDao.selectById(brandId);
        final CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
        log.info("保存完成");
    }

    /**
     * 更新关联表——品牌
     *
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        log.info("更新关联表——品牌");
        this.lambdaUpdate().set(CategoryBrandRelationEntity::getBrandName,name)
                .eq(CategoryBrandRelationEntity::getBrandId,brandId).update();
    }

    /**
     * 更新关联表——分类
     *
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {
        log.info("更新关联表——分类");
        this.lambdaUpdate().set(CategoryBrandRelationEntity::getCatelogName,name)
                .eq(CategoryBrandRelationEntity::getCatelogId,catId).update();
    }

}